<?php

namespace App\Console\Commands\Amqp;

use App\Common\Tools\DingTalk;
use Bschmitt\Amqp\Facades\Amqp;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class DingMsg extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'amqp:consume:ding:msg';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '发送钉钉消息';


    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $queue = config('amqp.ding.queue');
        Amqp::consume($queue, function ($message, $resolver) {
            $payload = json_decode($message->body, true);
            if (empty($payload)) {
                Log::warning('钉钉消息为空');
                $resolver->acknoledge($message);
                return;
            }
            $rs = DingTalk::getInstance()->to($payload['to'])->doSend($payload['content']);
            if ($rs) {
                echo '成功发送一条钉钉消息';
            }
            $resolver->acknowledge($message);
        }, [
            'exchange'            => config('amqp.ding.exchange'),
            'queue'               => config('amqp.ding.queue'),
            'queue_force_declare' => true,
        ]);
        return 0;
    }
}
